package com.sxj.PDMS.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxj.PDMS.model.dto.admin.MessageRequest;
import com.sxj.PDMS.model.dto.admin.MessageSelectRequest;
import com.sxj.PDMS.model.entity.DataInformationList;
import com.sxj.PDMS.model.entity.Message;
import com.sxj.PDMS.model.vo.MessageVO;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.MessageService;
import com.sxj.PDMS.mapper.MessageMapper;
import com.sxj.PDMS.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.sxj.PDMS.utils.VerifyUtil.formatDate;

/**
 * @author 503c'j
 * @description 针对表【message】的数据库操作Service实现
 * @createDate 2024-06-15 20:48:06
 */
@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
        implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Boolean messageUpload(MessageRequest messageRequest) throws ParseException {
        Message message = new Message();
        // 复制属性
        BeanUtils.copyProperties(messageRequest, message);

        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        message.setTime(formatDate(currentTime));
        // 保存消息
        boolean saved = this.save(message);
        return saved;
    }

    @Override
    public PagingQueryResponse<Message> selectAll(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<Message> messages = messageMapper.selectAll(start, pageSize);
        Long total = messageMapper.selectTotal();
        PagingQueryResponse<Message> pagingQueryResponse = new PagingQueryResponse<>();

        pagingQueryResponse.setRows(messages);

        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

    @Override
    public PagingQueryResponse<MessageVO> messageSelect(Integer page, Integer pageSize, MessageSelectRequest messageSelectRequest) {
        Integer start = (page - 1) * pageSize;
        String message = messageSelectRequest.getMessage();
        String adminId = messageSelectRequest.getAdminId();
        String studentNumber = messageSelectRequest.getStudentNumber();
        Date time = messageSelectRequest.getTime();
        String title = messageSelectRequest.getTitle();
        // 查询 Message 类型的列表
        List<Message> messages = messageMapper.selectByTitleOrMessageOrAdminIdOrStudentNumber(start, pageSize,title, message,time, adminId, studentNumber);
        Long total = messageMapper.selectTotalByTitleOrMessageOrAdminIdOrStudentNumber(title,message,time, adminId, studentNumber);



        // 将 Message 转换为 MessageVO
        List<MessageVO> messageVOs = messages.stream().map(msg -> {
            MessageVO vo = new MessageVO();
            vo.setId(String.valueOf(msg.getId()));
            vo.setStudentNumber(msg.getStudentNumber());
            vo.setTime(msg.getTime());
            vo.setAdminId(msg.getAdminId());
            vo.setMessage(msg.getMessage());
            vo.setFile(msg.getFile());
            vo.setTitle(msg.getTitle());
            return vo;
        }).collect(Collectors.toList());

        PagingQueryResponse<MessageVO> pagingQueryResponse = new PagingQueryResponse<>();
        pagingQueryResponse.setRows(messageVOs);
        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

}




